package com.example.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.domain.Demand;
import com.example.domain.Friend;
import com.example.domain.User;
import com.example.result.JsonResult;
import com.example.service.DemandService;
import com.example.service.FriendService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/demand")
public class DemandController extends BaseController{
    @Autowired
    private DemandService demandService;
    @Autowired
    private UserService userService;
    @Autowired
    private FriendService friendService;

    @PostMapping("/add")
    public JsonResult add(@RequestBody Demand demand){
        if(demand.getFid()==demand.getTid()){
            throw new RuntimeException("自己不能添加自己!");
        }
        QueryWrapper<Demand> wrapper=new QueryWrapper<>();
        wrapper.eq("fid",demand.getFid());
        wrapper.eq("tid",demand.getTid());
        Demand result = demandService.getOne(wrapper);
        if(result.getIs_add()==0){
            throw new RuntimeException("您已发送添加请求!");
        }
        if(result.getIs_add()==1){
            throw new RuntimeException("你们已是好友");
        }
        demand.setIs_add(0);demand.setAdd_time(new Date());
        boolean flag = demandService.save(demand);
        if(!flag){
            throw new RuntimeException("添加失败,请重试!");
        }
        return new JsonResult(true,"发送添加请求成功");
    }
    @GetMapping("/list")
    public JsonResult list(HttpSession session){
        Long sessionUId = getSessionUId(session);
        QueryWrapper<Demand> wrapper=new QueryWrapper<>();
        wrapper.eq("tid",sessionUId);
        wrapper.eq("is_add",0);
        wrapper.orderByDesc("add_time");
        List<Demand> list = demandService.list(wrapper);
        return new JsonResult(true,"",list);
    }
    //同意请求
    @PutMapping("/toAdd")
    public JsonResult toAdd(Long id,Long fromId,Long toId){
        Demand demand = demandService.getById(id);
        if(demand.getIs_add()==1){
            throw new RuntimeException("您已同意");
        }
        Demand de=new Demand();
        de.setIs_add(1);
        QueryWrapper<Demand> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);    //条件
        boolean update = demandService.update(de, wrapper);
        if(!update){
            throw new RuntimeException("请重试");
        }
        QueryWrapper<User> userWrapper1=new QueryWrapper<>();
        userWrapper1.eq("uid",fromId);
        User user1 = userService.getOne(userWrapper1);
        QueryWrapper<User> userWrapper2=new QueryWrapper<>();
        userWrapper2.eq("uid",toId);
        User user2 = userService.getOne(userWrapper2);
        if(user1==null||user2==null){
            throw new RuntimeException("请重试");
        }
        List<Friend> friends=new ArrayList<>();
        friends.add(new Friend(user1.getUid(),user2));
        friends.add(new Friend(user2.getUid(),user1));
        boolean flag = friendService.saveBatch(friends);
        if(!flag){
            throw new RuntimeException("请重试");
        }
        return new JsonResult(true,"");
    }
}
